// 基础edge，其他edge都在此基础上继承和扩展
import animation from './edge-animation';
import itemEvent from '../nodes/item-event';
import hvh_h from './hvh-h';
import hvh from './hvh';
import defaultStyles from '../defaultStyles';

// 绘制edge
function draw(cfg, group) {
    // 当前配置覆盖全局配置
    const attrs = Object.assign({}, {
        ...this.getShapeStyle(cfg),
        ...cfg
    });
    const keyShape = group.addShape('path', {
        className: 'edge-shape',
        name: 'edge-shape',
        attrs
    });
    return keyShape;
}
// 状态变动
function setState(name, value, item) {
    const edgeEvents = [
        'edgeState',
        'edgeState:default',
        'edgeState:selected',
        'edgeState:hover'
    ];
    const group = item.get('group');
    if (group.get('destroyed')) return;
    if (edgeEvents.includes(name)) {
        itemEvent[name].call(this, value, group);
    } else {
        console.warn(`warning: edge ${name} 事件回调未定义`);
    }
}

// 执行动画
function runAnimate(group, animationType) {
    if (this.running) return;
    this.running = true;
    group.toFront();
    animation[animationType].run.call(this, group);
}

// 停止并删除动画元素
function stopAnimate(group, animationType) {
    animation[animationType].stop.call(this, group);
    this.running = false;
}

// 继承edge
function inheritEdge(G6, edgeName) {
    G6.registerEdge(`${edgeName}-edge`, {
        running: true,
        runners: [],
        draw,
        setState,
        runAnimate,
        stopAnimate
    }, edgeName);
}

export default G6 => {
    const edgeArray = ['line', 'polyline', 'cubic', 'arc'];
    edgeArray.forEach(edge => {
        inheritEdge(G6, edge);
    });
    // 水平方向
    hvh(G6, {
        running: false,
        runners: [],
        setState,
        runAnimate,
        stopAnimate
    });
    // 垂直方向
    hvh_h(G6, {
        running: false,
        runners: [],
        setState,
        runAnimate,
        stopAnimate
    });
};
